#pragma once
#include <windows.h>
#include <string>
#include <list>
#include "tests/cefclient/others/JsonCpp/json/json.h"

namespace upupoo {
	namespace utility {

#define FILE_TEMP_POSTFIX L".tmp"
#define FILE_THEME L"theme.upup"
#define FILE_PREVIEW L"preview.jpg"
#define FILE_PAPER_ZIP L"paper.zip"
#define FILE_SIZE L"temp"

#define SECTION_VER "ver"
#define SECTION_SRC "src"
#define SECTION_AUTHOR_ID "author"
#define SECTION_AUTHOR_NAME "UserName"
#define SECTION_THEMENO "themeno"
#define SECTION_NAME "name"
#define SECTION_THEMETYPE "themeType"
#define SECTION_TAG "tag"
#define SECTION_URL "url"
#define SECTION_STATE "themeState"

		class CAutolock {
		public:
			CAutolock(LPCRITICAL_SECTION psection) {
				psection_ = psection;
				::EnterCriticalSection(psection);
			}

			~CAutolock() {
				::LeaveCriticalSection(psection_);
			}

			static void Init(LPCRITICAL_SECTION psection) {
				::InitializeCriticalSection(psection);
			}

			static void Delete(LPCRITICAL_SECTION psection) {
				::DeleteCriticalSection(psection);
			}

		protected:
			CRITICAL_SECTION* psection_;
		};

		std::string AnsiToUtf8(const char* src);

		int DeleteDirectory(const wchar_t* path);

		bool WriteSizeFileData(const wchar_t* path, long size);
		long ReadSizeFileData(const wchar_t* path);

		long GetFileSize(const wchar_t* path);
		
		std::string ReadJsonFileDataWipeBom(const wchar_t* path);

		std::string GetJsonStringA(const Json::Value& v, const char* section);
		std::wstring GetJsonStringW(const Json::Value& v, const char* section);

		int GetJsonInt(const Json::Value& v, const char* section);

		std::wstring GetConfigFile();

		bool WriteFile(const wchar_t* path, void* buf, int length);
		void UpDateJsonFile(const wchar_t* path, int state);
		std::string GetRootUrl();
		std::string GetPaperIdUrl();
		std::string GetUpLoadFinishedUrl();
		std::wstring GenerateLocalID();
		std::list<std::wstring> GetFolderPaths(const wchar_t* directory);
		bool CopyInDirectory(const wchar_t* pathfrom, const wchar_t* pathto);

		std::string UnicodeToUtf8(const wchar_t* src);

		unsigned char ToHex(unsigned char x);

		unsigned char FromHex(unsigned char x);

		std::string UrlEncode(const std::string& str);
		std::string UrlDecode(const std::string& str);
	}
}